package main

func subarraySum2(nums []int, k int) int {
	count := 0

	for i := 0; i < len(nums); i++ {
		temp := 0
		for j := i; j < len(nums); j++ {
			temp += nums[j]
			if temp == k {
				count++
			}
		}
	}

	return count
}

func subarraySum3(nums []int, k int) int {
	count := 0
	pre := make([]int, len(nums))
	m := make(map[int]int)
	m[0] = 1
	for i := 0; i < len(nums); i++ {
		if i == 0 {
			pre[i] = nums[i]
			m[pre[i]]++
			continue
		}

		pre[i] = pre[i-1] + nums[i]
		m[pre[i]]++
	}

	return count
}

func subarraySum(nums []int, k int) int {
	count := 0
	pre := make([]int, len(nums)+1)
	for i := 0; i < len(pre); i++ {
		if i == 0 {
			continue
		}
		pre[i] = pre[i-1] + nums[i-1]
	}

	for i := 0; i < len(pre); i++ {
		for j := i; j < len(pre); j++ {
			if j-i < 1 {
				continue
			}
			if pre[j]-pre[i] == k {
				count++
			}

		}

	}

	return count
}

func main() {
	subarraySum([]int{1, 1, 1}, 2)
}
